<?php
require_once 'Response.php';
require_once 'DB.php';

//1. 接受参数并简单验证
$username = empty($_REQUEST['username']) ? '' : $_REQUEST['username'];
$password = empty($_REQUEST['password']) ? '' : $_REQUEST['password'];

if(empty($username)) {
    Response::json(-1001, '请输入用户名！');
}

// 2. 数据库校验
// 2.1 查询用户
$pdo = DB::getInstance()->connect();
$stmt = $pdo->query("SELECT * FROM user WHERE username = '{$username}'"); //返回一个PDOStatement对象
$user = $stmt->fetch(); 

// var_dump($admin);
if(!$user) {
    Response::json(-1002, '用户名不存在！');
}

// 2.2 判断密码
if($user['password'] != $password) {
    Response::json(-1003, '密码不正确！');
}

Response::json(200, '登录成功！', $user);
